package com.shenzhen.horselive.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shenzhen.horselive.App;
import com.shenzhen.horselive.beans.CityCodeBean;
import com.shenzhen.horselive.beans.CodeBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class GetJsonDataUtil {

    private static GetJsonDataUtil  jsonDataUtil;
    private List<CityCodeBean>  cityCodeBeanList;

    private GetJsonDataUtil()
    {

    }

    public static GetJsonDataUtil getInstance() {
        if (jsonDataUtil == null) {
            jsonDataUtil = new GetJsonDataUtil();
        }
        return jsonDataUtil ;
    }

    public  String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public  void   initCityCode()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String  json = getJson(App.getInstance().getApplicationContext(),"city.json");
                Gson  gson = new Gson();
                cityCodeBeanList =  gson.fromJson(json,new TypeToken<List<CityCodeBean>>(){}.getType());
            }
        }).start();
    }

    public List<CityCodeBean> getCityCodeBeanList() {
        return cityCodeBeanList;
    }

    /**
     * 根据地名获取code
     * @return
     */
    public CodeBean getCode(String province,String  city,String  district)
    {
        if (TextUtils.isEmpty(province)||TextUtils.isEmpty(city)||TextUtils.isEmpty(district))
        {
            return null;
        }

        if (cityCodeBeanList==null||cityCodeBeanList.size()==0)
        {
            return null;
        }

        CodeBean  codeBean = new CodeBean();
        for (CityCodeBean b :
                cityCodeBeanList) {
            if (b.getName().equals(province))
            {
                codeBean.setProvinceCode(b.getCode());
                //城市
                for (CityCodeBean.CityListBean b1:b.getCityList()
                     ) {

                    if (b1.getName().equals(city))
                    {
                        codeBean.setCityCode(b1.getCode());
                        for (CityCodeBean.CityListBean.AreaListBean b2:b1.getAreaList()
                             ) {
                            if (b2.getName().equals(district))
                            {
                                codeBean.setDistrictCode(b2.getCode());
                                break;
                            }
                        }
                        break;
                    }
                }

                break;
            }
        }

     return  codeBean;

    }



}
